// we7/pages/mine/mineOrderEvaluate.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_evaluate_content:"",
    imgList: [],
    // goods_image:"",
    // 评价图片
    // evaluationImgUrl: "https://s1.ax1x.com/2018/08/05/PDM8Bj.png",
    starCheckedImgUrl: "https://s1.ax1x.com/2018/08/05/PDQ0it.png",
    starUnCheckedImgUrl: "https://s1.ax1x.com/2018/08/05/PDQdII.png",
    // 各项评分
    threePing:[
      "描述相符",
      "快递物流服务",
      "服务态度"
    ],
    // 建议内容
    opinion: "",

    starMap: [
      '非常差',
      '差',
      '一般',
      '好',
      '非常好',
    ],
// 店铺评分
    evaluations: [
      {
        id: 0,
        name: "描述相符",
        image: "https://s1.ax1x.com/2018/08/05/PDMaCV.png",
        star: 0,
        note: ""
      },
      {
        id: 1,
        name: "服务态度",
        image: "https://s1.ax1x.com/2018/08/05/PDMN40.png",
        star: 0,
        note: ""
      }
    ],
    // 满意度
    evaluationa: [
      {
        id: 0,
        name: "医院环境",
        image: "https://s1.ax1x.com/2018/08/05/PDMaCV.png",
        star: 0,
        note: ""
      }
    ],
    goods_evaluate_image:[],
  },
  /**
 * 评分
 */
  chooseStar: function (e) {
    const index = e.currentTarget.dataset.index;
    const star = e.target.dataset.star;
    let evaluationa = this.data.evaluationa;
    let evaluation = evaluationa[index];
    evaluation.star = star;
    evaluation.note = this.data.starMap[star - 1];
    console.log(evaluationa[0].star)
    this.setData({
      evaluationa: evaluationa
    })
  },
  chooseStars: function (e) {
    const index = e.currentTarget.dataset.index;
    const star = e.target.dataset.star;
    let evaluations = this.data.evaluations;
    let evaluation = evaluations[index];
    evaluations[index]['star'] = star;
    evaluation.star = star;
    evaluation.note = this.data.starMap[star - 1];
    this.setData({
      evaluations: evaluations
    })
  },
  ViewImage(e) {
    console.log(e)
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除图片',
      content: '确定要删除这张图片吗？',
      cancelText: '保留',
      confirmText: '删除',
      success: res => {
        // console.log(res)
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  ChooseImage() {
    wx.chooseImage({
      count: 1, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        // console.log(res)
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  // textarea
  textareaBind(e){
    console.log(e.detail.value)
    this.setData({
      goods_evaluate_content: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      goods_id: options.goods_id,
      order_id:options.order_id
    })
    wx.setNavigationBarTitle({
      title: '我的评价',
    })
    
  },
  fabu:function(){
    var that = this
    const value = wx.getStorageSync('openid');
    for(var i=0;i<that.data.imgList.length;i++){
      console.log(that.data.imgList)
      wx.uploadFile({
        url: app.util.url("admin/WxGoodsApi/uploadImg") + "&m=dwq_pinpinpin",
        filePath: that.data.imgList[i],
        name: 'img',
        formData: {
          'imgIndex': 1
        },
        header: {
          "Content-Type": "multipart/form-data"
        },
        success: function (res) {
          var goods_evaluate_image = that.data.goods_evaluate_image;
         
          var data = JSON.parse(res.data);
          goods_evaluate_image.push(data.data);
          that.setData({
            goods_evaluate_image: goods_evaluate_image
          })
          
          if (that.data.imgList.length == goods_evaluate_image.length){
            app.util.request({
              url: 'admin/WxGoodsApi/addEvaluate',
              data: {
                goods_id: that.data.goods_id,
                order_id: that.data.order_id,
                uid: value,
                goods_evaluate_star: that.data.evaluationa[0].star,
                goods_evaluate_content: that.data.goods_evaluate_content,
                goods_evaluate_image: that.data.goods_evaluate_image,
                goods_describe_star: that.data.evaluations[0].star,
                shop_service_star: that.data.evaluations[1].star
              },
              method: 'post',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                // console.log(res)
                if(res.data.code==0){
                  wx.redirectTo({
                    url: 'mineBalanceRechargeSuccessful?code=0'
                  })
                }else{
                  wx.redirectTo({
                    url: 'mineBalanceRechargeSuccessful?code=1'
                  })
                }
                

              },
              fail: function (res) {
                wx.showToast({
                  title: '服务器响应失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            });
          }
        }
      })
    }
    
    

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})